package com.ysd.jdbc;

import com.ysd.lis.entity.DataBaseBo;
import lombok.SneakyThrows;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class JdbcConnectionManager {
//    private static Connection connection = null;
//    private static Statement statement = null;
//    private static JdbcConnectionManager instance = null;
//
//    private JdbcConnectionManager() {
//    }
//
//    public static JdbcConnectionManager getInstance() {
//        if (instance == null) {
//            synchronized (JdbcConnectionManager.class) {
//                if (instance == null) {
//                    instance = new JdbcConnectionManager();
//                }
//            }
//        }
//        return instance;
//    }


    @SneakyThrows
    public Connection getConnection(DataBaseBo dataBaseBo) {
        Class.forName(dataBaseBo.getDriverClassName());
        return DriverManager.getConnection(dataBaseBo.getUrl(), dataBaseBo.getUsername(), dataBaseBo.getPassword());
    }


    public List<Map<String, Object>> executeQuery(Connection connection, String sql) {

        ResultSet resultSet = null;
        Statement statement = null;
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            List<Map<String, Object>> maps = resultSetToMap(resultSet);
            return maps;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public List<Map<String, Object>> executeQuery(Connection connection, String sql, String... params) {
        ResultSet resultSet = null;
        try {
            PreparedStatement prepared = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                prepared.setString(i + 1, params[i]);
            }
            resultSet = prepared.executeQuery();
            List<Map<String, Object>> maps = resultSetToMap(resultSet);
            return maps;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private static List<Map<String, Object>> resultSetToMap(ResultSet rs) throws SQLException {
        List<Map<String, Object>> resultList = new ArrayList<>();
        ResultSetMetaData metaData = rs.getMetaData();
        int columnCount = metaData.getColumnCount();

        while (rs.next()) {
            Map<String, Object> row = new HashMap<>();
            for (int i = 1; i <= columnCount; i++) {
                String columnName = metaData.getColumnLabel(i);
                Object value = rs.getObject(i);
                row.put(columnName, value);
            }
            resultList.add(row);
        }
        return resultList;
    }

    @SneakyThrows
    public void closeConnection(Connection connection) {
        if (connection != null) {
            connection.close();
        }
    }

}
